﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using NetfractionHub.LocalMachine.BotCommands;
using NetfractionHub.Network.Protocol;
using NetfractionHub.Network.Protocol.BASE.Factory;

namespace NetfractionHub.LocalMachine
{
    [BotNode(CommandPrefix="!")]
    public class ChannelBot : BotNode
    {
        private List<RemoteMachine.Node> members;

        public ChannelBot()
        {
            /*AddCommand(new BotCommands.Handler.ListPermissions());
            AddCommand(new BotCommands.Handler.ResetPassword());
            AddCommand(new BotCommands.Handler.AddConnectionListener());*/

            //CreateBotCIDS(); // Generate a CID for the security and operator bots
            this.Identity.ClientId = Settings.HubSettings.OperatorBot.ClientId;
            //this.Identity.Description = Settings.HubSettings.SecurityBot.Description;
            this.Identity.NodeType = IdentityNodeTypes.Bot;
            Version curVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
            this.Identity.ClientVersion = String.Format("Netfraction Bot {0}.{1}", curVersion.Major, curVersion.Minor);
            this.Identity.MaximumUploadSpeed = 100 * 1024 * 1024;
            //this.Identity.HubOperatorCount = 1;
            this.SecurityIdentity = Security.Identity.CreateGenericIdentityForRole(Security.RoleManager.GetRoleById(6));
            this.Identity.Nickname = "Special Chat Channel";

            members = new List<NetfractionHub.RemoteMachine.Node>();

            NetfractionHub.Network.ConnectionManager.SendNodeInformationToAll(this);
        }

        private static void CreateBotCIDS()
        {
            /* Generate a CID for the security and operator bots
            Network.Protocol.TIGR.Security.Cryptography.Tiger hashAlgorithm = Network.Protocol.TIGR.Security.Cryptography.Tiger.Create();
            hashAlgorithm.Initialize();
            byte[] hash = hashAlgorithm.ComputeHash(System.Text.Encoding.UTF8.GetBytes("the security bot private id"));
            System.Diagnostics.Debug.WriteLine(Base32.Encode(hash).Substring(0, 39));
            hash = hashAlgorithm.ComputeHash(System.Text.Encoding.UTF8.GetBytes("the operator bot private id"));
            System.Diagnostics.Debug.WriteLine(Base32.Encode(hash).Substring(0, 39));*/
        }

        public override void HandleChatMessage(RemoteMachine.Node sourceNode, ProtocolMessage protoMessage)
        {
            string messageText = string.Empty;
            var messageParam = protoMessage.Parameters.Where(c => string.IsNullOrEmpty(c.ParameterCode));
            if (messageParam.Count() > 0)
            {
                messageText = messageParam.First().ParameterValue;
            }
            else
            {
                return;
            }
            Console.WriteLine(messageText);
        }
    }
}
